package com.example.gamecenter.fragments.home

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.example.gamecenter.R
import com.example.gamecenter.activitys.BaseTencentWebActivity
import com.example.gamecenter.activitys.SearchActivity
import com.example.gamecenter.activitys.WebviewActivity
import kotlinx.android.synthetic.main.activity_home_page.*

class HomeFragment: Fragment(R.layout.activity_home_page) {
    // context
    private lateinit var fragmentContext: Context;
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState);
        fragmentContext = (context ?: this) as Context;

        // 创建一个新的 FragmentTransaction
        val fragmentTransaction =  childFragmentManager.beginTransaction();
        // 在 FragmentTransaction 中添加子 Fragment .add(fragment_container, recommendFragment)
        fragmentTransaction.add(R.id.home_fragment_container, HomeRecommendFragment())
        // 提交 FragmentTransaction
        fragmentTransaction.commit()
        // 初始化
        initView(view);
    }

    fun initView(view: View){
//        val tab_recommend = view.findViewById<TextView>(R.id.tab_recommend);
//        val tab_classify = view.findViewById<TextView>(R.id.tab_classify);
//        val tab_ranking = view.findViewById<TextView>(R.id.tab_ranking);
//        val tab_special_column = view.findViewById<TextView>(R.id.tab_special_column);
        tv_search.setOnClickListener {
            val searchActivity = SearchActivity() // 初始化
            val intent = Intent(fragmentContext, SearchActivity().javaClass)
            startActivity(intent)
        }
        /** 精选 */
        tab_recommend.setOnClickListener {
            tab_recommend.textSize = 24f;
            tab_recommend.setTextColor(ContextCompat.getColor(fragmentContext, R.color.text_color));

            tab_classify.textSize = 16f;
            tab_classify.setTextColor(ContextCompat.getColor(fragmentContext, R.color.hui_color))
            tab_ranking.textSize = 16f;
            tab_ranking.setTextColor(ContextCompat.getColor(fragmentContext, R.color.hui_color))
            tab_special_column.textSize = 16f;
            tab_special_column.setTextColor(ContextCompat.getColor(fragmentContext, R.color.hui_color))
            // 切换fragment
            val fragmentTransaction =  childFragmentManager.beginTransaction();
            // 设置转场动画
            fragmentTransaction.setCustomAnimations(R.animator.fragment_slide_left_enter,
                R.animator.fragment_slide_right_exit)
            fragmentTransaction.replace(R.id.home_fragment_container, HomeRecommendFragment());
            fragmentTransaction.commit();
        }

        tab_classify.setOnClickListener {
            tab_classify.textSize = 24f;
            tab_classify.setTextColor(ContextCompat.getColor(fragmentContext, R.color.text_color));

            tab_recommend.textSize = 16f;
            tab_recommend.setTextColor(ContextCompat.getColor(fragmentContext, R.color.hui_color))
            tab_ranking.textSize = 16f;
            tab_ranking.setTextColor(ContextCompat.getColor(fragmentContext, R.color.hui_color))
            tab_special_column.textSize = 16f;
            tab_special_column.setTextColor(ContextCompat.getColor(fragmentContext, R.color.hui_color))
            // 切换fragment
//            val fragmentTransaction =  childFragmentManager.beginTransaction();
//            // 设置转场动画
//            fragmentTransaction.setCustomAnimations(R.animator.fragment_slide_left_enter,
//                R.animator.fragment_slide_right_exit)
//            fragmentTransaction.replace(R.id.home_fragment_container, HomeClassifyFragment());
//            fragmentTransaction.commit();

            val intent = Intent(fragmentContext, WebviewActivity().javaClass)
            startActivity(intent)
        }
//        tab_ranking.setOnClickListener {
//            tab_ranking.textSize = 24f;
//            tab_ranking.setTextColor(ContextCompat.getColor(fragmentContext, R.color.text_color));
//
//            tab_recommend.textSize = 16f;
//            tab_recommend.setTextColor(ContextCompat.getColor(fragmentContext, R.color.hui_color))
//            tab_classify.textSize = 16f;
//            tab_classify.setTextColor(ContextCompat.getColor(fragmentContext, R.color.hui_color))
//            tab_special_column.textSize = 16f;
//            tab_special_column.setTextColor(ContextCompat.getColor(fragmentContext, R.color.hui_color))
//            // 切换fragment
//            val fragmentTransaction =  childFragmentManager.beginTransaction();
//            // 设置转场动画
//            fragmentTransaction.setCustomAnimations(R.animator.fragment_slide_left_enter,
//                R.animator.fragment_slide_right_exit)
//            val homeRankingFragment :HomeRankingFragment = HomeRankingFragment();
//            fragmentTransaction.replace(R.id.home_fragment_container, homeRankingFragment);
//            fragmentTransaction.commit();
//        }
        tab_special_column.setOnClickListener {
            tab_special_column.textSize = 24f;
            tab_special_column.setTextColor(ContextCompat.getColor(fragmentContext, R.color.text_color));

            tab_recommend.textSize = 16f;
            tab_recommend.setTextColor(ContextCompat.getColor(fragmentContext, R.color.hui_color))
            tab_ranking.textSize = 16f;
            tab_ranking.setTextColor(ContextCompat.getColor(fragmentContext, R.color.hui_color))
            tab_classify.textSize = 16f;
            tab_classify.setTextColor(ContextCompat.getColor(fragmentContext, R.color.hui_color))
            // 切换fragment
            val fragmentTransaction =  childFragmentManager.beginTransaction();
            // 设置转场动画
            fragmentTransaction.setCustomAnimations(R.animator.fragment_slide_left_enter,
                R.animator.fragment_slide_right_exit)
            fragmentTransaction.replace(R.id.home_fragment_container, HomeSpecialColumnFragment());
            fragmentTransaction.commit();
        }
    }
}